package com.abc.cloud.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.abc.cloud.common.utils.JwtUtil;
import com.abc.cloud.entity.FileEntity;
import com.abc.cloud.vo.file.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.abc.cloud.entity.FileShareEntity;
import com.abc.cloud.service.FileShareService;
import com.abc.cloud.common.utils.PageUtil;
import com.abc.cloud.common.utils.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 文件分享表
 *
 * @author LiJunXi
 * @email 2770063826@qq.com
 * @date 2023-10-27 13:09:54
 */
@Api(tags = "文件分享接口")
@RestController
@RequestMapping("/fileShare")
public class FileShareController {
    @Autowired
    private FileShareService fileShareService;

    /**
     * 查询用户分享文件列表
     */
    @ApiOperation(value = "查询用户分享文件列表")
    @GetMapping("/list")
    public Result list(@RequestParam Map<String, Object> params){
        PageUtil page = fileShareService.queryPage(params);

        return Result.success(page, "page");
    }

    /**
     * 查询分享文件信息
     */
    @ApiOperation(value = "查询分享文件信息")
    @GetMapping("/info/{uuid}")
    public Result<Map<String, Object>> info(@PathVariable("uuid") String uuid,
                                            HttpServletRequest httpServletRequest){
        Map<String, Object> fileShare = fileShareService.info(uuid, httpServletRequest);

        return Result.success(fileShare, "查询分享文件信息成功");
    }

    /**
     * 查询分享具体文件列表
     * @param params
     * @return
     */
    @ApiOperation(value = "查询分享具体文件列表")
    @GetMapping("/getFile")
    public Result<PageUtil> getFile(@RequestParam Map<String, Object> params,
                                    HttpServletRequest httpServletRequest){
        PageUtil pageUtil = fileShareService.getFileShare(params, httpServletRequest);

        return Result.success(pageUtil, "获取分享文件成功");
    }

    /**
     * 保存分享文件
     * @param fileShare
     */
    @ApiOperation(value = "保存分享文件")
    @PostMapping("/share")
    public Result<Map<String, String>> shareFile(@RequestBody FileShare fileShare){
        Map<String, String> map = fileShareService.shareFile(fileShare);

        if(map == null) return Result.fail(null, "分享文件出错");
        else return Result.success(map, "分享文件成功");
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改分享文件")
    @PutMapping("/update")
    public Result update(@RequestBody FileShareUpdate fileShareUpdate){
		fileShareService.updateShare(fileShareUpdate);

        return Result.success(null, "操作成功");
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除分享文件")
    @PutMapping("/delete")
    public Result delete(@RequestBody Long[] ids){
		fileShareService.removeByIds(Arrays.asList(ids));

        return Result.success(null, "");
    }

    /**
     * 验证提取码
     * @param fileShareCheck
     * @return
     */
    @ApiOperation(value = "验证提取码")
    @PostMapping("/check")
    public Result check(@RequestBody FileShareCheck fileShareCheck,
                        HttpServletRequest httpServletRequest){
        Boolean flag = fileShareService.check(fileShareCheck, httpServletRequest);

        if(flag) return Result.success(null, "验证成功");
        else return Result.success(null, "验证码错误");
    }

    /**
     * 下载分享文件
     * @param response
     * @param uuid
     */
    @ApiOperation(value = "下载分享文件")
    @GetMapping("/download/{id}")
    public void download(HttpServletResponse response,
                         HttpServletRequest request,
                         @PathVariable("id") Long id,
                         @RequestParam("uuid") String uuid,
                         @CookieValue(value = "token", required = false) String token){
        Long userId;
        if (token != null) userId = JwtUtil.getUserId(token);
        else userId = null;

        fileShareService.download(response, uuid, id, request, userId);
    }

    /**
     * 管理员查询分享文件列表
     */
    @ApiOperation(value = "管理员查询分享文件列表")
    @GetMapping("/admin/list")
    public Result adminQueryList(@RequestParam Map<String, Object> params){
        PageUtil page = fileShareService.adminQueryList(params);

        return Result.success(page, "page");
    }

    /**
     * 管理员删除文件（更新文件状态）
     * @param fileIds
     * @return
     */
    @ApiOperation(value = "管理员删除文件（更新文件状态）")
    @PutMapping("/admin/updateStatus/{status}")
    public Result adminDeleteFile(@RequestBody List<Long> fileIds,
                                  @PathVariable("status") Integer status){
        fileShareService.adminDeleteFile(fileIds, status);

        return Result.success(null, "操作成功");
    }

    /**
     * 管理员更新文件分享信息
     * @param fileShareAdminUpdate
     * @return
     */
    @ApiOperation(value = "管理员更新文件分享信息")
    @PutMapping("/admin/updateShareFile")
    public Result adminUpdateShareFile(@RequestBody FileShareAdminUpdate fileShareAdminUpdate){
        fileShareService.adminUpdateShareFile(fileShareAdminUpdate);

        return Result.success(null, "操作成功");
    }


}
